import DataBus from '../databus'

/**
 * 悔棋功能测试
 */
export default class UndoTest {
  constructor() {
    this.databus = new DataBus()
    this.testResults = []
  }

  /**
   * 运行所有测试
   */
  runAllTests() {
    console.log('开始运行悔棋功能测试...')
    
    this.testPlayerUndoInAIMode()
    this.testAIUndoDisabled()
    this.testUndoReturnsToPlayer()
    this.testUndoCountDecreases()
    this.testUndoClearsPendingMove() // 新增测试
    
    this.printResults()
  }

  /**
   * 测试AI模式下玩家可以悔棋
   */
  testPlayerUndoInAIMode() {
    console.log('测试：AI模式下玩家悔棋')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    this.databus.currentPlayer = 1 // 玩家执黑
    
    // 模拟玩家落子
    this.databus.board[7][7] = 1
    this.databus.lastMove = { row: 7, col: 7 }
    this.databus.currentPlayer = 2 // 切换到AI
    
    // 尝试AI悔棋（应该失败）
    const initialUndoCount = this.databus.getUndoCount(2)
    this.databus.useUndo(2)
    const afterUndoCount = this.databus.getUndoCount(2)
    
    const result = afterUndoCount === initialUndoCount
    this.testResults.push({
      name: 'AI模式下玩家悔棋',
      passed: result,
      message: result ? '测试通过' : '测试失败：AI悔棋次数不应该减少'
    })
  }

  /**
   * 测试AI模式下AI不能悔棋
   */
  testAIUndoDisabled() {
    console.log('测试：AI不能悔棋')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    this.databus.currentPlayer = 2 // AI执白
    
    // 模拟AI落子
    this.databus.board[8][8] = 2
    this.databus.lastMove = { row: 8, col: 8 }
    
    // 尝试AI悔棋（应该失败）
    const canUndo = this.databus.canUndo(2)
    
    const result = !canUndo
    this.testResults.push({
      name: 'AI不能悔棋',
      passed: result,
      message: result ? '测试通过' : '测试失败：AI应该不能悔棋'
    })
  }

  /**
   * 测试悔棋后回到玩家回合
   */
  testUndoReturnsToPlayer() {
    console.log('测试：悔棋后回到玩家回合')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    this.databus.currentPlayer = 2 // AI执白
    
    // 模拟AI落子
    this.databus.board[8][8] = 2
    this.databus.lastMove = { row: 8, col: 8 }
    
    // 模拟玩家悔棋
    this.databus.board[8][8] = 0
    this.databus.lastMove = null
    this.databus.currentPlayer = 1 // 切换回玩家
    
    const result = this.databus.currentPlayer === 1
    this.testResults.push({
      name: '悔棋后回到玩家回合',
      passed: result,
      message: result ? '测试通过' : '测试失败：悔棋后应该回到玩家回合'
    })
  }

  /**
   * 测试悔棋次数减少
   */
  testUndoCountDecreases() {
    console.log('测试：悔棋次数减少')
    
    const player = 1
    const initialCount = this.databus.getUndoCount(player)
    
    // 使用一次悔棋
    this.databus.useUndo(player)
    const afterCount = this.databus.getUndoCount(player)
    
    const result = afterCount === initialCount - 1
    this.testResults.push({
      name: '悔棋次数减少',
      passed: result,
      message: result ? '测试通过' : '测试失败：悔棋次数应该减少1'
    })
  }

  testUndoClearsPendingMove() {
    console.log('测试：悔棋后清除待确认的落子')
    
    // 创建模拟的board对象
    const mockBoard = this.createMockBoard()
    
    // 模拟有pendingMove的状态
    mockBoard.pendingMove = { row: 7, col: 7, player: 1 }
    mockBoard.previewMove = { row: 7, col: 7, player: 1 }
    
    // 调用clearPreview
    mockBoard.clearPreview()
    
    const result = mockBoard.pendingMove === null && mockBoard.previewMove === null
    this.testResults.push({
      name: '悔棋后清除待确认的落子',
      passed: result,
      message: result ? '测试通过' : '测试失败：clearPreview应该同时清除pendingMove和previewMove'
    })
  }

  /**
   * 创建模拟的DataBus
   */
  createMockDataBus() {
    return {
      isAIMode: false,
      aiPlayer: 2,
      currentPlayer: 1,
      board: Array(15).fill().map(() => Array(15).fill(0)),
      lastMove: null,
      undoCounts: { 1: 3, 2: 3 },
      
      canUndo(player) {
        return this.undoCounts[player] > 0
      },
      
      useUndo(player) {
        if (this.undoCounts[player] > 0) {
          this.undoCounts[player]--
          return true
        }
        return false
      },
      
      getUndoCount(player) {
        return this.undoCounts[player] || 0
      }
    }
  }

  createMockBoard() {
    return {
      pendingMove: null,
      previewMove: null,
      clearPreview() {
        this.previewMove = null
        this.pendingMove = null
      }
    }
  }

  /**
   * 打印测试结果
   */
  printResults() {
    console.log('\n=== 悔棋功能测试结果 ===')
    let passedCount = 0
    let totalCount = this.testResults.length
    
    this.testResults.forEach((test, index) => {
      const status = test.passed ? '✅ 通过' : '❌ 失败'
      console.log(`${index + 1}. ${test.name}: ${status}`)
      if (!test.passed) {
        console.log(`   原因: ${test.message}`)
      } else {
        passedCount++
      }
    })
    
    console.log(`\n总计: ${passedCount}/${totalCount} 测试通过`)
    
    if (passedCount === totalCount) {
      console.log('🎉 所有测试通过！悔棋功能正常。')
    } else {
      console.log('⚠️  部分测试失败，需要检查代码。')
    }
  }
} 